package com.sgb.kjwl.view.widget.transport;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.MaterChooseEntity;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.ui.adapter.MatterChooseAdapter5;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

//单选下拉框样式三
public class MatterChooseWindow extends BasePopWindow {

    private View linearLayout;
    private RecyclerView recyclerView;
    private MatterChooseAdapter5 dictAdapter;
    private List<MaterChooseEntity.TypeBean> dictEntityList;
    private OnCallBack onCallBack;
    private OnCloseBack onCloseBack;
    private int type;
    private View view,view1;
    private RelativeLayout mLlPopWindowDict5;

    public MatterChooseWindow(Context context) {
        super(context);
    }
    public MatterChooseWindow(Context context, int type) {
        super(context);
        this.type = type;
    }
    @Override
    protected int popLayout() {
        return R.layout.popwindow_dict5;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        linearLayout = holder.getPop().findViewById(R.id.root_layout);
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        mLlPopWindowDict5 = holder.getPop().findViewById(R.id.rl_pop_window_dict5);
        view = holder.getPop().findViewById(R.id.v_pop_window_dict5_left);
        view1 = holder.getPop().findViewById(R.id.v_pop_window_dict5_right);
        view1.setVisibility(View.INVISIBLE);
        view.setVisibility(View.INVISIBLE);
        bgView.setOnClickListener(v -> dissMiss());
        dictAdapter = new MatterChooseAdapter5(mContext, null);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(dictAdapter);
        dictAdapter.setOnItemClick(new AdapterOnItemClick<MaterChooseEntity.TypeBean>() {
            @Override
            public void onItemClick(MaterChooseEntity.TypeBean dictEntity, int position) {
                if (onCallBack != null) {
                    onCallBack.result(dictEntity);
                }
                dissMiss();
            }
        });
    }

    public void setData(List<MaterChooseEntity.TypeBean> dictEntityList) {
        this.dictEntityList = dictEntityList;
        dictAdapter.setStringName(type);
        dictAdapter.setDatas(dictEntityList);
    }

    public void setCheckDefaultByName(String checkedId) {
        if (dictEntityList == null || dictEntityList.size() == 0) {
            return;
        }
        for (MaterChooseEntity.TypeBean dictEntity3 : dictEntityList) {
            if (dictEntity3.getCname().equals(checkedId)) {
                dictEntity3.setChecked(true);
            } else {
                dictEntity3.setChecked(false);
            }
        }
        dictAdapter.setStringName(type);
        dictAdapter.setDatas(dictEntityList);
    }

    public void setCheckDefaultById(String checkedId) {
        if (dictEntityList == null || dictEntityList.size() == 0 || checkedId == null) {
            return;
        }
        for (MaterChooseEntity.TypeBean dictEntity3 : dictEntityList) {
            if (dictEntity3.getCid().equals(checkedId)) {
                dictEntity3.setChecked(true);
            } else {
                dictEntity3.setChecked(false);
            }
        }
        dictAdapter.setStringName(type);
        dictAdapter.setDatas(dictEntityList);
    }

    public int getCheckDefultPosition(String checkedId) {
        if (dictEntityList == null || dictEntityList.size() == 0) {
            return -1;
        }
        for (int i = 0; i < dictEntityList.size(); i++) {
            if (dictEntityList.get(i).getCid().equals(checkedId)) {
                return i;
            }
        }
        return -1;
    }

    public String getCheckDefultId(String checkedId) {
        if (dictEntityList == null || dictEntityList.size() == 0) {
            return "";
        }
        for (int i = 0; i < dictEntityList.size(); i++) {
            if (dictEntityList.get(i).getCid().equals(checkedId)) {
                return dictEntityList.get(i).getCid();
            }
        }
        return "";
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCloseBack != null) {
            onCloseBack.dismiss();
        }
    }

    public interface OnCallBack {
        void result(MaterChooseEntity.TypeBean dictEntity);
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnCloseBack {
        void dismiss();
    }

    public void setOnCloseBack(OnCloseBack onCloseBack) {
        this.onCloseBack = onCloseBack;
    }

    public void setPickQuestion(){
        linearLayout.setBackgroundColor(getContentView().getContext().getResources().getColor(R.color.color_main_bg));
    }

    public void setPadding(Context context,int rightMargin,int leftMargin,boolean isDP){
        ViewGroup.MarginLayoutParams linearLayout = (ViewGroup.MarginLayoutParams) mLlPopWindowDict5.getLayoutParams();
//        LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.leftMargin = isDP ? DisplayUtil.dip2px(context,leftMargin) : leftMargin;
        linearLayout.rightMargin = isDP ? DisplayUtil.dip2px(context,rightMargin) : rightMargin;
        mLlPopWindowDict5.setLayoutParams(linearLayout);
    }

    public void setVisible(boolean isVisible){
        if (isVisible){
            view1.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
        }else {
            view1.setVisibility(View.INVISIBLE);
            view.setVisibility(View.INVISIBLE);
        }
    }
}
